package logic

import (
	"bw-renting/model/mysql"
	"bw-renting/user-rpc/internal/svc"
	"bw-renting/user-rpc/user"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
	return &UserRegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// Todo:用户注册
func (l *UserRegisterLogic) UserRegister(in *user.UserRegisterReq) (*user.Response, error) {
	// todo: add your logic here and delete this line
	User := mysql.User{
		Mobile: in.Mobile,
	}
	//查找手机号是否存在
	result := mysql.SearchMobile(User)

	//已存在算作的登录
	if result.ID != 0 {
		return &user.Response{
			UserId: int64(result.ID),
		}, nil
	}
	//进行注册
	res := mysql.CreateUser(User)
	return &user.Response{
		UserId: int64(res.ID),
	}, nil
}
